/*
 *  Copyright (C) 2025 ameise <ameise.wang@gmail.com> - All Rights Reserved
 *
 *  This file is part of e3utils.
 *
 *  e3utils is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  e3utils is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with e3utils. If not, see <https://www.gnu.org/licenses/>.
 */

package utils

import (
	"hash/crc64"

	"gitee.com/ameise84/e3utils/cache"
	"gitee.com/ameise84/e3utils/str_conv"
)

var _gHashMap64 cache.Cache[string, uint64]

func init() {
	_gHashMap64 = cache.New[string, uint64](cache.DefaultOptions().SetSize(4096).SetTTL(0))
}

func Hash64(s string) uint64 {
	if len(s) == 0 {
		return 0
	}
	if v, ok := _gHashMap64.Load(s); ok {
		return v
	}

	h := crc64.MakeTable(crc64.ECMA)
	n := crc64.Checksum(str_conv.ToBytes(s), h)
	_gHashMap64.Store(s, n)
	return n
}

func Hash32(s string) uint64 {
	return Hash64(s) % (1 << 32)
}

func Hash16(s string) uint64 {
	return Hash64(s) % (1 << 16)
}
